package com.amex.uam.action;

import java.sql.Timestamp;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.amex.uam.model.AMEX_BUSINESS;
import com.amex.uam.model.AMEX_UNIT;
import com.amex.uam.model.MS_USER_ADMIN;
import com.amex.uam.model.Message;
import com.amex.uam.model.USER_DEPT;
import com.amex.uam.service.AmexBusService;
import com.amex.uam.service.AmexUnitService;
import com.amex.uam.service.DeptService;
import com.amex.uam.service.UserService;
import com.amex.uam.util.Utils;
import com.microstrategy.web.beans.RequestKeys;

@Controller("editUserAction")
public class EditUserAction {
	
	public static boolean DEBUG = true;
	
	private UserService userService;
	private DeptService deptService;
	private AmexBusService amexBusService;
	private AmexUnitService amexUnitService;
	
	@Resource
	public void setAmexBusService(AmexBusService amexBusService) {
		this.amexBusService = amexBusService;
	}
	@Resource
	public void setAmexUnitService(AmexUnitService amexUnitService) {
		this.amexUnitService = amexUnitService;
	}

	@Resource
	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Message editUser(RequestKeys keys) {
		
		/* edit user general information */
		editUserGeneralInfo(keys);
		addDepartment(keys);
		addBusiness(keys);
		addAmexUnit(keys);
		return new Message(Message.OK);
	}
	
	public boolean addDepartment(RequestKeys keys) {
		USER_DEPT dept = new USER_DEPT();
		dept.setDEPT_ID(val(keys, "deptId"));
		dept.setDEPT_NM(val(keys, "deptDesc"));
		log("addDepartment", "deptId=" + dept.getDEPT_ID() + ", deptDesc=" + dept.getDEPT_NM());
		
		Message msg = deptService.updateDepartment(dept);
		return msg.getStatus() == Message.OK;
	}
	public boolean addBusiness(RequestKeys keys) {
		AMEX_BUSINESS bus = new AMEX_BUSINESS();
		bus.setAMEX_BUS_CD(val(keys, "bussUnitCD"));
		bus.setAMEX_BUS_DS_TX(val(keys, "bussUnitDesc"));
		log("addBusiness", "bussUnitCD=" + bus.getAMEX_BUS_CD() + ", bussUnitDesc=" + bus.getAMEX_BUS_DS_TX());
		
		Message msg = amexBusService.updateBusiness(bus);
		return msg.getStatus() == Message.OK;
	}
	
	public boolean addAmexUnit(RequestKeys keys) {
		AMEX_UNIT unit = new AMEX_UNIT();
		unit.setAMEX_UNIT_CD(val(keys, "amexUnitCD"));
		unit.setAMEX_UNIT_DS_TX(val(keys, "amexUnitDesc"));
		log("addAmexUnit", "amexUnitCD=" + unit.getAMEX_UNIT_CD() + ", amexUnitDesc=" + unit.getAMEX_UNIT_DS_TX());
		
		Message msg = amexUnitService.updateAmexUnit(unit);
		return msg.getStatus() == Message.OK;
	}
	public boolean editUserGeneralInfo(RequestKeys keys) {
		MS_USER_ADMIN user =new MS_USER_ADMIN();
		user.setUSER_ID(val(keys,"empId"));
    	user.setWORKFORCE_MBR_ID(val(keys,"amexWebId"));
		user.setDEPT_ID(val(keys,"deptId"));
		user.setAMEX_BUS_CD(val(keys,"bussUnitCD"));
		user.setUSER_EMAIL_AD_TX(val(keys,"emailAddr"));
		user.setWORKFORCE_MBR_MAN_1ST_NM(val(keys,"mFName"));
		user.setWORKFORCE_MBR_MAN_MI_TX(val(keys,"mMidInit"));
		user.setWORKFORCE_MBR_MAN_LST_NM(val(keys,"mLName"));
		user.setAMEX_UNIT_CD(val(keys,"amexUnitCD"));
		user.setCOST_CTR_CD(val(keys,"costCenter"));
		user.setUSER_CMPL_PHONE_NO(val(keys,"phone"));
		user.setWORKFORCE_MBR_MAN_EMAIL_AD_TX(val(keys,"mgrEmailAddr"));
		user.setVP_NM(val(keys,"VPName"));
		user.setUSER_1ST_NM(val(keys,"firstName"));
    	user.setUSER_MI_TX(val(keys,"midName"));
    	user.setUSER_LST_NM(val(keys,"lastName"));
		user.setACSS_RSN_TX(val(keys,"reason4Accs")); 
		user.setUSER_ACSS_TYPE_CD(val(keys, "accType"));
		//user.setCREAT_TS(cREAT_TS);
		
		
		log("addUser", user.toString());
		
		user.setLST_LOGIN_TS(new Timestamp(System.currentTimeMillis()));
    	user.setLST_UPDT_TS(new Timestamp(System.currentTimeMillis()));
    	user.setCREAT_TS(new Timestamp(System.currentTimeMillis()));
    	user.setSEC_XCPT_END_DT(new Timestamp(System.currentTimeMillis()));
    	user.setNDA_TS(new Timestamp(System.currentTimeMillis()));
		
       	user.setUSER_ADMIN_STA_CD("0");
    	user.setUSER_STA_CD("0");
    	user.setUSER_GRP_ID(0);
    	user.setUSER_TYPE_CD("0");
    	user.setGEO_ACSS_LVL_CD("G");
    	user.setLIF_GLBL_ACS_IN("N");
    	user.setWORKFORCE_MBR_CLASS_CD("C");
    	user.setCLM_GLBL_ACS_IN("Y");
    	user.setUSER_ACSS_REJCT_RSN_TX("");
    	//user.setFUNC_GRP_ROLE_ID(keys.getValueCount("funtcRoleStep2"));
    	Message msg = userService.editUserGeneralInfo(user);
		return msg.getStatus() == Message.OK;
		
		 
 	}
	
	private String val(RequestKeys keys, String paramName) {
		return val(keys, paramName, null);
	}
	private String val(RequestKeys keys, String paramName, String defVal) {
		return Utils.trim(keys.getValue(paramName), defVal);
	}
	private void log(String methodName, String message) {
		if (DEBUG)
			System.out.println(this.getClass().getSimpleName() + "->" + methodName + ":  " + message);
	}

}
